﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using System.Text.RegularExpressions;

namespace FingerVein_Database_Collection
{
    public partial class User_Information_Form : Form
    {
        MainForm mf;

        public User_Information_Form(MainForm mform)
        {
            mf = mform;
            InitializeComponent();
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            // ID is self generated. No checking needed
            MainForm.UserID = txtUserID.Text;
            MainForm.UserName = txtName.Text;

            var checkedButton = gbUserinfo.Controls.OfType<RadioButton>()
                                  .FirstOrDefault(r => r.Checked);
            MainForm.UserGender = checkedButton.Text;
            MainForm.UserStatus = txtStatus.Text;
            MainForm.UserNationality = txtNationality.Text;

            int age;
            if (int.TryParse(txtAge.Text, out age))
            {
                MainForm.UserAge = age.ToString();
            }
            else
            { MessageBox.Show("Age format is not correct"); }

            Regex myRegularExpression = new Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (myRegularExpression.IsMatch(txtEmail.Text))
            {
                MainForm.UserEmail = txtEmail.Text;
            }
            else
            { MessageBox.Show("Email format is not correct"); }

            if (MainForm.UserID == null || MainForm.UserName == null || MainForm.UserAge == null || MainForm.UserEmail == null || MainForm.UserStatus == null || MainForm.UserNationality == null)
            {
                MessageBox.Show("Please fill in all the required information");
            }
            else
            { this.Close(); }


            //if ( txtName.Text == "" || txtAge.Text == "" || txtEmail.Text == "" || txtStatus.Text == ""||txtNationality.Text == "")
            //{
            //    MessageBox.Show("Please fill in all the required information");
            //}

            //else
            //{
            //    MainForm.UserID = txtUserID.Text; 
            //    MainForm.UserName = txtName.Text;

            //    var checkedButton = gbUserinfo.Controls.OfType<RadioButton>()
            //                          .FirstOrDefault(r => r.Checked);
            //    MainForm.UserGender = checkedButton.Text;
            //    MainForm.UserEmail = txtEmail.Text;
            //    MainForm.UserAge = txtAge.Text;
            //    MainForm.UserStatus = txtStatus.Text;
            //    MainForm.UserNationality = txtNationality.Text;

            //    DAL.USER_TABLE user = new DAL.USER_TABLE();
            //    user.User_Name = txtName.Text;
            //    user.User_Gender = checkedButton.Text;
            //    user.User_Email = txtEmail.Text;
            //    user.User_Status = txtStatus.Text;
            //    user.User_Nationality = txtNationality.Text;

            //    UserManager us = new UserManager();
            //    us.SaveUser(user);

                
            //}

            //this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            mf.Close();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            Load_User_Form form = new Load_User_Form();
            form.ShowDialog();
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
